package swind.web.service.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import swind.web.dao.ISubjectDao;
import swind.web.dao.ITestDao;
import swind.web.dao.ITestTypeDao;
import swind.web.entities.ExamSubject;
import swind.web.entities.ExamTest;
import swind.web.entities.ExamTestType;
import swind.web.service.IManageTestService;
import swind.web.vo.TestTypeAndSubjectVo;
import swind.web.vo.TestSubjectVo;
import swind.web.vo.TestTypeVo;
import swind.web.vo.TestUpdateVo;
import swind.web.vo.TestVo;

@Service("manageTestServiceImp")
@Transactional
public class ManageTestServiceImp implements IManageTestService{
	
	@Resource(name="testDaoImp")
	ITestDao testDaoImp;
	
	@Resource(name="subjectDaoImp")
	ISubjectDao subjectDaoImp;
	
	@Resource(name="testTypeDaoImp")
	ITestTypeDao testTypeDaoImp;
	
	
	@Override
	public List<TestVo> findAll() {
		List<TestVo> voList = new ArrayList<TestVo>();
		List<ExamTest> testList= testDaoImp.loadAll();
		for(ExamTest test:testList){
			TestVo testVo = new TestVo();
			TestTypeVo testTypeVo = new  TestTypeVo();
			TestSubjectVo testSubjectVo = new TestSubjectVo();
			BeanUtils.copyProperties(test, testVo);
			BeanUtils.copyProperties(test.getSubject(), testSubjectVo);
			BeanUtils.copyProperties(test.getTestType(), testTypeVo);
			testVo.setSubjectVo(testSubjectVo);
			testVo.setTestTypeVo(testTypeVo);
			voList.add(testVo);
		}
		return voList;
		
	}

	@Override
	public TestTypeAndSubjectVo getAllTypeAndSubject() {
		// TODO Auto-generated method stub
	List<ExamTestType> typeList= testTypeDaoImp.loadAll();
	List<ExamSubject> subjectList= subjectDaoImp.loadAll();
	
	List<TestTypeVo> typeVoList = new ArrayList<TestTypeVo>();
	List<TestSubjectVo> subjectVoList = new ArrayList<TestSubjectVo>();
	for(ExamTestType examType : typeList){
		TestTypeVo type = new TestTypeVo();
		BeanUtils.copyProperties(examType, type);
		typeVoList.add(type);
	}
	for(ExamSubject examSubject : subjectList){
		TestSubjectVo subject = new TestSubjectVo();
		BeanUtils.copyProperties(examSubject, subject);
		subjectVoList.add(subject);
	}
	TestTypeAndSubjectVo testTypeAndSubjectVo = new TestTypeAndSubjectVo();
	testTypeAndSubjectVo.setSubjectList(subjectVoList);
	testTypeAndSubjectVo.setTypeList(typeVoList);
	
	return testTypeAndSubjectVo;
	}
	
	
	@Override
	public TestTypeVo getScoreType(int typeId) {
		// TODO Auto-generated method stub
		TestTypeVo type = new TestTypeVo();
		ExamTestType examTestType = testTypeDaoImp.get(typeId);
		BeanUtils.copyProperties(examTestType, type);
		return type;
	}
	

	@Override
	public void addTest(TestVo testVo) {
		// TODO Auto-generated method stub
		ExamTest examTest = new ExamTest();
		ExamTestType examTestType = testTypeDaoImp.get(testVo.getTestTypeVo().getId());
		ExamSubject examSubject = subjectDaoImp.get(testVo.getSubjectVo().getSubId());
		testVo.setId(0);//防止Integer为null转换为int时报错
		BeanUtils.copyProperties(testVo, examTest);
		examTest.setSubject(examSubject);
		examTest.setTestType(examTestType);
		
		testDaoImp.save(examTest);
	}

	@Override
	public TestUpdateVo getTestById(int testId) {
		ExamTest examTest = testDaoImp.get(testId);
		TestSubjectVo testSubjectVo = new TestSubjectVo();
		TestTypeVo testTypeVo = new TestTypeVo();
		TestUpdateVo testUpdateVo = new TestUpdateVo();
		BeanUtils.copyProperties(examTest.getSubject(), testSubjectVo);
		BeanUtils.copyProperties(examTest.getTestType(), testTypeVo);
		BeanUtils.copyProperties(examTest, testUpdateVo);
		testUpdateVo.setSubjectVo(testSubjectVo);
		testUpdateVo.setTestTypeVo(testTypeVo);
		
		TestTypeAndSubjectVo testTypeAndSubjectVo = this.getAllTypeAndSubject();
		
		testUpdateVo.setSubjectList(testTypeAndSubjectVo.getSubjectList());
		testUpdateVo.setTypeList(testTypeAndSubjectVo.getTypeList());
		
		return testUpdateVo;
	}

	@Override
	public void updateTest(TestVo testVo) {
		ExamTest examTest = new ExamTest();
		ExamTestType examTestType = testTypeDaoImp.get(testVo.getTestTypeVo().getId());
		ExamSubject examSubject = subjectDaoImp.get(testVo.getSubjectVo().getSubId());
		
		BeanUtils.copyProperties(testVo, examTest);
		examTest.setSubject(examSubject);
		examTest.setTestType(examTestType);
		
		testDaoImp.update(examTest);
		
	}

	@Override
	public void deleteTest(int id) {
		
		testDaoImp.deleteByKey(id);
	}

	



	
}
